Skip to content

Method: static {...}

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.rdf4j.list;
19:
20: import cz.cvut.kbss.ontodriver.descriptor.SimpleListDescriptor;
21: import cz.cvut.kbss.ontodriver.descriptor.SimpleListValueDescriptor;
22: import cz.cvut.kbss.ontodriver.model.Axiom;
23: import cz.cvut.kbss.ontodriver.model.NamedResource;
24: import cz.cvut.kbss.ontodriver.rdf4j.connector.RepoConnection;
25: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
26: import org.eclipse.rdf4j.model.IRI;
27: import org.eclipse.rdf4j.model.Resource;
28: import org.eclipse.rdf4j.model.Statement;
29: import org.eclipse.rdf4j.model.ValueFactory;
30:
31: import java.util.ArrayList;
32: import java.util.Collection;
33: import java.util.Iterator;
34: import java.util.List;
35: import java.util.Set;
36:
37: public class SimpleListHandler extends ListHandler<SimpleListValueDescriptor> {
38:
39: public SimpleListHandler(RepoConnection connector, ValueFactory vf) {
40: super(connector, vf);
41: }
42:
43: /**
44: * Loads axioms representing list described by the specified list descriptor.
45: *
46: * @return Collection of axioms representing sequence values
47: * @throws Rdf4jDriverException When storage access error occurs
48: */
49: public List<Axiom<NamedResource>> loadList(SimpleListDescriptor listDescriptor) throws Rdf4jDriverException {
50: final List<Axiom<NamedResource>> axioms = new ArrayList<>();
51: final ListIterator<NamedResource> it = new SimpleListIterator(listDescriptor, connector, vf);
52: while (it.hasNext()) {
53: axioms.add(it.nextAxiom());
54: }
55: return axioms;
56: }
57:
58: @Override
59: protected IRI createListHead(SimpleListValueDescriptor listValueDescriptor, Collection<Statement> listStatements) {
60: final IRI firstNode = toRdf4jIri(listValueDescriptor.getValues().get(0).getIdentifier());
61: listStatements.add(vf.createStatement(owner(listValueDescriptor), hasList(listValueDescriptor),
62: firstNode, context(listValueDescriptor)));
63: return firstNode;
64: }
65:
66: @Override
67: protected List<Statement> createListRest(IRI head, SimpleListValueDescriptor listValueDescriptor) {
68: final List<Statement> statements = new ArrayList<>(listValueDescriptor.getValues().size());
69: IRI previous = head;
70: final IRI nextNodeProp = hasNext(listValueDescriptor);
71: final IRI context = context(listValueDescriptor);
72: final Iterator<NamedResource> it = listValueDescriptor.getValues().iterator();
73: it.next();
74: while (it.hasNext()) {
75: final IRI object = toRdf4jIri(it.next().getIdentifier());
76: statements.add(vf.createStatement(previous, nextNodeProp, object, context));
77: previous = object;
78: }
79: return statements;
80: }
81:
82: /**
83: * We are using this code instead of iterator.remove for performance reasons. The iterator has to reconnect the list
84: * for each removed node, which takes a lot of time.
85: */
86: protected void clearList(SimpleListValueDescriptor listValueDescriptor) throws Rdf4jDriverException {
87: final Set<IRI> contexts = contexts(listValueDescriptor);
88: final Collection<Statement> toRemove = new ArrayList<>();
89: IRI currentProperty = hasList(listValueDescriptor);
90: final IRI hasNext = hasNext(listValueDescriptor);
91: final boolean includeInferred = listValueDescriptor.getNextNode().isInferred();
92: Collection<Statement> stmts;
93: Resource subject = owner(listValueDescriptor);
94: do {
95: stmts = connector.findStatements(subject, currentProperty, null, includeInferred, contexts);
96: if (!stmts.isEmpty()) {
97: subject = extractListNode(stmts, hasNext);
98: toRemove.addAll(stmts);
99: }
100: currentProperty = hasNext;
101: } while (!stmts.isEmpty());
102: connector.removeStatements(toRemove);
103: }
104:
105: protected void mergeList(SimpleListValueDescriptor listDescriptor) throws Rdf4jDriverException {
106: final ListIterator<NamedResource> it = iterator(listDescriptor);
107: final ListHandler.MergeResult mergeResult = mergeWithOriginalList(listDescriptor, it);
108: removeObsoletes(it);
109: assert mergeResult.i > 0;
110: assert mergeResult.previous != null;
111: if (mergeResult.i < listDescriptor.getValues().size()) {
112: appendNewNodes(listDescriptor, mergeResult);
113: }
114: }
115:
116: MergeResult mergeWithOriginalList(SimpleListValueDescriptor listDescriptor, ListIterator<NamedResource> it) throws
117: Rdf4jDriverException {
118: int i = 0;
119: Resource node = null;
120: while (it.hasNext() && i < listDescriptor.getValues().size()) {
121: node = it.nextNode();
122: final NamedResource newNode = listDescriptor.getValues().get(i);
123: if (!node.stringValue().equals(newNode.getIdentifier().toString())) {
124: node = toRdf4jIri(newNode.getIdentifier());
125: it.replaceCurrentWith(newNode);
126: }
127: i++;
128: }
129: return new MergeResult(i, node);
130: }
131:
132: void appendNewNodes(SimpleListValueDescriptor listDescriptor, MergeResult mergeResult) throws
133: Rdf4jDriverException {
134: int i = mergeResult.i;
135: final Collection<Statement> toAdd = new ArrayList<>(listDescriptor.getValues().size() - i);
136: Resource previous = mergeResult.previous;
137: final IRI nextNode = toRdf4jIri(listDescriptor.getNextNode().getIdentifier());
138: final IRI context = context(listDescriptor);
139: while (i < listDescriptor.getValues().size()) {
140: final Resource newNode = toRdf4jIri(listDescriptor.getValues().get(i).getIdentifier());
141: final Statement stmt = vf.createStatement(previous, nextNode, newNode, context);
142: toAdd.add(stmt);
143: previous = newNode;
144: i++;
145: }
146: connector.addStatements(toAdd);
147: }
148:
149: private ListIterator<NamedResource> iterator(SimpleListValueDescriptor listDescriptor) throws Rdf4jDriverException {
150: return new SimpleListIterator(listDescriptor, connector, vf);
151: }
152: }